﻿using Cysharp.Threading.Tasks;
using UnityEngine;
using YooAsset;

namespace Client.Base
{
    public class FsmUpdatePackageManifest : PatchState
    {
        private bool isSucc = true;
        public override async void OnEnter()
        {
            this.Log("[Patch] FsmUpdatePackageManifest.");
            isSucc = true;
            string[] packages = AppInfo.Packages;
            for (int i = 0; i < packages.Length; i++)
                await UpdateManifest(packages[i]);

            if (!isSucc)
            {
                PatchHandle.Instance.ShowErrorMessage("初始化失败(402)");
                return;
            }
            PatchHandle.Instance.Change<FsmCreateDownloader>();
        }

        private async UniTask UpdateManifest(string packageName)
        {
            string packageVersion = PatchHandle.Instance.GetPackageVersion(packageName);
            var package = YooAssets.GetPackage(packageName);
            var operation = package.UpdatePackageManifestAsync(packageVersion);
            await operation.ToUniTask();

            if (operation.Status != EOperationStatus.Succeed)
            {
                isSucc = false;
                this.Log($"[Patch]: Fail ！-> pk:{package} , error : {operation.Error}");
            }
            else
            {

            }
        }
    }
}

